package dfs_bfs.test216;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * 组合总数三
 *
 * @author code_yc
 * @version 1.0
 * @date 2020/7/9 10:14
 */
public class Solution1 {

    List<List<Integer>> res = new ArrayList<>();
    ArrayDeque<Integer> path = new ArrayDeque<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        dfs(k, n, 0, 1);
        return res;
    }

    private void dfs(int k, int n, int s, int c) {
        if (s == n) {
            if (path.size() == k) {
                res.add(new ArrayList<>(path));
            }
            return;
        }
        if (s > n) return;

        for (int i = c; i <= 9; i++) {
            path.addLast(i);
            dfs(k, n, s + i, i + 1);
            path.pollLast();
        }
    }
}
